#define _CRT_SECURE_NO_WARNINGS

using namespace std;
#include "printer_bugs.h"

void PrinterBugs::OnTestProgramStart(const UnitTest& )
{
    ::std::fstream file;
    file.open(FileName(), ::std::ios_base::out|::std::ios_base::trunc );
    if(file.fail())
        SetBroken();
}


void PrinterBugs::OnTestStart(const TestInfo& test_info)
{
    mCaseName=test_info.test_case_name();
    mTestName=test_info.name();
}


void PrinterBugs::OnTestPartResult(const TestPartResult& test_part_result)
{
    if( IsBroken() || !test_part_result.failed() ) 
        return;

    ::std::fstream file(FileName(), ::std::ios_base::out|::std::ios_base::app );
    if(file.fail())
        return SetBroken();

    file<<'\n'<< mCaseName<<"."<<mTestName<< " "<< MessageFailed()<<'\n'
         <<test_part_result.summary()<<'\n'
         <<MessageFileName()<<" : "<< test_part_result.file_name()<< '\n'
         <<MessageLine()<<" : "<< test_part_result.line_number()<< '\n'<< endl;
}


void PrinterBugs::OnTestProgramEnd(const UnitTest& unit_test)
{
    if( IsBroken() || IsDeathTest() )
        return;

    ::std::fstream file(FileName(), ::std::ios_base::out|::std::ios_base::app );
    if(file.fail())
        return SetBroken();

    //const size_t nRun        = unit_test.test_to_run_count();
    const size_t nTotalCase  = unit_test.total_test_case_count();
    const size_t nSuccesCase = unit_test.successful_test_case_count();
    const size_t nTotalTest  = unit_test.total_test_count();
    const size_t nSuccesTest = unit_test.successful_test_count();
    const size_t nDisable    = unit_test.disabled_test_count();
    const size_t nFailed     = nTotalTest-nSuccesTest-nDisable;
    const size_t nCaseFailed = nTotalCase-nSuccesCase;
    file <<'\n'
         <<MessageTotalFailedCases()<<nCaseFailed <<'\n'
         <<MessageTotalFailedTests()<<nFailed    <<'\n';

    if(nDisable>0) 
        file<<MessageTotalDisabledTests()<<nDisable<<"\n\n";

    file<<MessageTotalPucket() << (unit_test.Passed()? MessagePucketSuccess(): MessagePucketFailed() )<<endl;
    
}